import { Component, Input, OnInit } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";

import { ModalController } from "@ionic/angular";

import { CustomValidators } from "../../shared/custom-validators/custom-validators";
import { AppGlobalService } from "../../shared/service/app-global.service";
import { EventService } from "../../shared/service/event.service";
import { HttpService } from "../../shared/service/http.service";
import { MapBaiduService } from "../../shared/service/map.baiduapi";
import { Platform } from '@ionic/angular';
declare const Umeng;

@Component({
  selector: 'app-mobile-phone-verification',
  templateUrl: './mobile-phone-verification.component.html',
  styleUrls: ['./mobile-phone-verification.component.scss'],
})
export class MobilePhoneVerificationComponent implements OnInit {
  constructor(
    private platform: Platform,
    private modalCtrl: ModalController,
    private fb: FormBuilder,
    private http: HttpService,
    private appGlobalService: AppGlobalService,
    private mapBaiduService: MapBaiduService,
    private eventService: EventService
  ) { }
  @Input() tagData: any = {};
  sessionId: string;
  open: boolean;

  registerForm: FormGroup;
  mobileNum: any;
  mvcodeNum: any;
  usernameNum: any;
  passwordNum: any;
  passwordNum1: any;
  open1: any;

  positionInfo: any;

  cityId: any;
  cityName: any;

  time = -1;
  text = "获取验证码";
  mvcode: any;

  ngOnInit() {

    console.log(this.tagData);
    this.sessionId = localStorage.getItem("sessionId");
    const parttern = "[A-Za-z0-9]{6,12}";
    this.registerForm = this.fb.group({
      account: [
        "",
        [
          Validators.required,
          Validators.minLength(11),
          Validators.maxLength(11),
          CustomValidators.phone
        ]
      ],
      mvcode: ["", [Validators.required, CustomValidators.vcode]]
    });

    const that = this;
    setTimeout(() => {
      that.positionInfo = JSON.parse(
        this.mapBaiduService.getStore("locationObj")
      )
        ? JSON.parse(this.mapBaiduService.getStore("locationObj"))
        : that.gotoPage();
      // that.cityId = that.mapBaiduService.getCityId(that.positionInfo.name);
    }, 1000);
  }

  getErrors() {
    if (this.registerForm.controls.account.errors) {
      if (this.registerForm.controls.account.errors.required) {
        this.appGlobalService.showToast("请输入手机号码", 3000, "top");
      } else {
        this.appGlobalService.showToast("请输入正确的手机号码", 3000, "top");
      }
    } else if (this.registerForm.controls.mvcode.errors) {
      if (this.registerForm.controls.mvcode.errors.required) {
        this.appGlobalService.showToast("请输入验证码", 3000, "top");
      } else {
        this.appGlobalService.showToast("请输入正确的验证码", 3000, "top");
      }
    }
  }

  submit() {
    if (this.registerForm.status === "INVALID") {
      this.getErrors();
      return;
    }

    const paramArray = this.tagData.sureButton.actionElement.param;
    const paramLen = paramArray.length;
    let opportunityId = "";
    for (let i = 0; i < paramLen; i++) {
      const temp = paramArray[i];
      if (temp && temp.name === "opportunity_id") {
        opportunityId = temp.value;
        Object.assign(this.registerForm.value, { opportunity_id: opportunityId });
      }
    }

    const param = {
      content: Object.assign(this.registerForm.value),
      id: this.tagData.sureButton.id,
      sessionId: this.sessionId
    };
    param.content.city_id = JSON.parse(this.mapBaiduService.getStore("position")).code;
    this.http.post("application/submit", param, data => {
      if (data.success) {
        const loginObj = {
          pageId: data.target,
          params: data.params,
          sessionId: this.sessionId
        };
        this.appGlobalService.showToast(data.message, 3000, "middle");
        this.appGlobalService.goToForward(loginObj);
      } else {
        this.appGlobalService.showToast(data.message, 3000, "top");
      }
    });
  }


  getCity() {
    const city = localStorage.getItem("currentCityPosition");
    this.cityId = JSON.parse(city).id;
    console.log(this.cityId, "cityid");
  }

  goToLogin() {
    const loginObj = {
      pageId: this.tagData.login.link.target,
      params: {},
      sessionId: this.sessionId
    };
    this.appGlobalService.goToForward(loginObj);
  }
  gotoPage() {
    console.log('msg----->请选择城市');
  }
  getVC(invalid) {
    console.log(this.registerForm.get("account"));
    if (invalid) {
      this.appGlobalService.showToast("请输入正确手机号码", 3000, "top");
    } else {
      const params = {
        content: {
          mobile: this.registerForm.value.account
        },
        // tslint:disable-next-line:no-string-literal
        id: this.tagData.codeButton["id"],
        sessionId: localStorage.getItem("sessionId")
      };
      this.http.post("application/submit", params, data => {
        if (data.success) {
          this.appGlobalService.showToast(
            "验证码已发送、请注意查收",
            3000,
            "top"
          );
          if (data.data) {
            this.mvcode = data.data.mvcode;
          }
          this.time = 90;
          const timer = setInterval(() => {
            this.time = this.time - 1;
            if (this.time === -1) {
              clearInterval(timer);
              this.text = "获取验证码";
            }
          }, 1000);
        } else {
          this.appGlobalService.showToast(data.message, 3000, "top");
        }
      });
    }
  }
}
